//
//  NSString+Check.m
//  MyCheZan
//
//  Created by lovecar on 14-7-30.
//  Copyright (c) 2014年 chezan. All rights reserved.
//

#import "NSString+Check.h"
#import<CommonCrypto/CommonDigest.h>

@implementation NSString (Check)

- (BOOL)checkPhoneNumInput{
    NSString * mobile = @"^[1][358]\\d{9}$";
    NSPredicate *regextestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobile];
    BOOL res1 = [regextestMobile evaluateWithObject:self];
    if (res1){
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)checkEmailInput {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *regexEmail = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [regexEmail evaluateWithObject:self];
}

- (BOOL)checkUserName{
    NSString * regex = @"^[A-Za-z0-9\u4e00-\u9fa5]{1,30}$";//http://www.jb51.net/article/72867.htm
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:self];
}

- (BOOL)checkUserPassword{
    NSString * regex = @"^[A-Za-z\u4e00-\u9fa5]{1,12}$";//http://www.jb51.net/article/72867.htm
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:self];
}

- (NSString *)md5:(NSString *)input{
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return  output;
}

@end
